
import cantools
import os

class dbcParse_MessagePackFileGenerate:
    def __init__(self):
        pass

    # 创建CAN报文打包函数.c文件
    def __create_messagepack_c_file(self, dbc_file_pathname, dbc_ecu_name):
        dbc_info = cantools.db.load_file(dbc_file_pathname)
        dbc_c_file_name = dbc_file_pathname.replace('.dbc', '_MessagePack.c')
        dbc_name = dbc_file_pathname.replace('.dbc', '')
        file_folder = 'src_generate'
        if not os.path.exists(file_folder):
            os.mkdir(file_folder)
        dbc_c_file_pathname = os.path.join(file_folder, dbc_c_file_name)
        with open(dbc_c_file_pathname, 'w', encoding='utf-8') as file_c:
            line_chars = '#include \"' + dbc_file_pathname.replace('.dbc', '_MessagePack.h') + '\"'
            file_c.write(line_chars + '\n\n')
            for message in dbc_info.messages:
                if(dbc_ecu_name in message.senders):
                    # 写入函数名
                    line_chars = 'void ' + dbc_file_pathname.replace('.dbc', '_') + message.name + '_Pack(uint8 * pData, ' + dbc_name + '_' + message.name.upper() + '_STRUCT *p' + message.name + ')\n{'
                    file_c.write(line_chars + '\n')
                    for signal in message.signals:
                        sigPbcfgName = dbc_file_pathname.replace('.dbc', '') + '_' + message.name + '_' + signal.name  + '_PbCfg'
                        sigValName = '(void *)(&(p' + message.name + '->' + 'sig_' + signal.name + '_Val)' + ')'
                        line_chars = '    dbcParse_CanSignalEncode(&' + sigPbcfgName + ', pData, ' + sigValName + ');'
                        file_c.write(line_chars + '\n')
                    
                    line_chars = '}\n'
                    file_c.write(line_chars + '\n')
                    

    # 创建CAN报文打包函数.h文件
    def __create_messagepack_h_file(self, dbc_file_pathname, dbc_ecu_name):
        dbc_info = cantools.db.load_file(dbc_file_pathname)
        dbc_h_file_name = dbc_file_pathname.replace('.dbc', '_MessagePack.h')
        dbc_name = dbc_file_pathname.replace('.dbc', '')
        file_folder = 'src_generate'
        if not os.path.exists(file_folder):
            os.mkdir(file_folder)
        dbc_h_file_pathname = os.path.join(file_folder, dbc_h_file_name)
        with open(dbc_h_file_pathname, 'w', encoding='utf-8') as file_h:
            line_chars = '#include \"' + dbc_file_pathname.replace('.dbc', '_SignalsPbconfig.h') + '\"'
            file_h.write(line_chars + '\n\n')
            for message in dbc_info.messages:
                if(dbc_ecu_name in message.senders):
                    # 写入函数名
                    line_chars = 'extern void ' + dbc_file_pathname.replace('.dbc', '_') + message.name + '_Pack(uint8 * pData, ' + dbc_name + '_' + message.name.upper() + '_STRUCT *p' + message.name + ');\n'
                    file_h.write(line_chars + '\n')


    def create_messagepack_files(self, dbc_file_pathname, dbc_ecu_name):
        self.__create_messagepack_c_file(dbc_file_pathname, dbc_ecu_name)
        self.__create_messagepack_h_file(dbc_file_pathname, dbc_ecu_name)

